#include <iostream>
#include <fstream>
#include <stdio.h>
#include "user.pb.h"

using namespace std;
using namespace user;

bool AddUser(user::UserList &userList) {
    cout << "------------ 新增用户 ------------" << endl;

    user::User *user = userList.add_users(); // 创建新用户并添加到列表

    cout << "请输入您的姓名：";
    string name;
    cin.ignore(); // 清除前一个输入的换行符
    getline(cin, name);
    user->set_name(name);

    cout << "请输入您的邮箱：";
    string email; cin >> email;
    user->set_email(email);

    cout << "请输入您的性别(1. MALE 2. FEMALE)" << endl;
    int gender;
    cin >> gender;
    if (gender == 1) {
        user->set_gender(User::MALE);
    } else if (gender == 2) {
        user->set_gender(User::FEMALE);
    }

    cout << "请输入您的联系方式（1. 电话号码 2. QQ号）" << endl;
    int contactType = 0;
    cin >> contactType;
    switch(contactType) {
        case 1: {
            cout << "请输入您的电话号码：";
            string phoneNumber; cin >> phoneNumber; cin.ignore();
            user->set_phone_number(phoneNumber);
            break;
        } case 2: {
            cout << "请输入您的QQ号：";
            string qqNumber; cin >> qqNumber; cin.ignore();
            user->set_qq_number(qqNumber);
            break;
        } default: {
            cout << "无效的联系方式类型" << endl;
            return false;
        }
    }

    cout << "请输入成绩【科目-成绩】(quit - 退出)" << endl;
    while (true) {
        string subject, score;
        cout << "请输入科目：";
        cin >> subject;
        if (subject == "quit") {
            break;
        }
        cout << "请输入成绩：";
        cin >> score;
        if (score == "quit") {
            break;
        }
        user->mutable_achievement()->insert({subject, score});
    }

    ArticleList articleList;
    for (int i = 1;; i++) {
        cout << "输入文章信息 (quit: 退出)" << i << endl;
        string title, content;
        cout << "请输入文章标题：";
        cin.ignore();
        getline(cin, title);
        if (title == "quit") {
            cout << "------------ 退出成功 ------------" << endl;
            break;
        }
        cout << "请输入文章内容：";
        getline(cin, content);
        if (content == "quit") {
            cout << "------------ 退出成功 ------------" << endl;
            break;
        }

        Article *article = articleList.add_articles(); // 将文章添加到文章列表中
        article->set_title(title);
        article->set_content(content);
    }

    user->mutable_article_list()->CopyFrom(articleList); // 使用 mutable_article_list() 修改这行
    cout << "------------ 添加用户成功 ------------" << endl;
    return true;
}

int main(int argc, char *argv[]) {
    GOOGLE_PROTOBUF_VERIFY_VERSION; // 检查版本号
    if (argc != 2) {
        cerr << "Usage: " << argv[0] << " FILENAME" << endl;
        return -1;
    }

    UserList userList; // 创建 UserList 对象

    // 1. 从文件中读取用户列表
    fstream input(argv[1], ios::in | ios::binary);
    if (!input) {
        cerr << "Failed to open file: " << argv[1] << endl;
    } else {
        if (!userList.ParseFromIstream(&input)) {
            cerr << "Failed to parse user list." << endl;
            return -1;
        }
    }

    // 2. 添加用户
    while (true) {
        AddUser(userList);
        cout << "是否继续添加用户？(y/n)" << endl;
        char choice;
        cin >> choice;
        if (choice == 'n' || choice == 'N') {
            break;
        }
    }

    // 3. 将用户列表写入文件
    fstream output(argv[1], ios::out | ios::trunc | ios::binary);
    if (!output) {
        cerr << "Failed to open file: " << argv[1] << endl;
    } else {
        if (!userList.SerializeToOstream(&output)) {
            cerr << "Failed to write user list." << endl;
            return -1;
        }
    }

    // 4. 清理
    input.close();
    output.close();
    google::protobuf::ShutdownProtobufLibrary(); // 释放资源

    return 0;
}
